package com.gxuwz.restaurant.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.restaurant.common.Result;
import com.gxuwz.restaurant.entity.Role;

import com.gxuwz.restaurant.mapper.RoleMapper;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    RoleMapper roleMapper;



    @GetMapping("/select")   //get用来查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        System.out.println("执行查询角色管理");
        LambdaQueryWrapper<Role> wrapper = Wrappers.<Role>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Role::getRole,search);
        }

        Page<Role> rolePage =  roleMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);


        return Result.success(rolePage );
     }

    @PostMapping //post用来新增
    public Result<?> save(@RequestBody Role role){
        System.out.println("执行添加权限管理");

        roleMapper.insert(role);

        return Result.success();
    }

    @PutMapping //put用来更新
    public Result<?> update (@RequestBody Role role){
        System.out.println("执行更新");

        roleMapper.updateById(role);

        return Result.success();


    }

    @DeleteMapping("/{id}") //DELETE用来删除
    public Result<?> delete(@PathVariable long id){
        System.out.println("执行删除方法");
        roleMapper.deleteById(id);

        return Result.success();


    }


}
